import { apiWrapper } from '@/utils/request'

// ========================= 管理员客户管理API (/Admin/customer) =========================
export const adminCustomerApi = {
  // 管理员登录
  login: (loginData) => {
    return apiWrapper.post('/Admin/customer/login', loginData)
  },

  // 获取管理员信息
  getAdminInfo: () => {
    return apiWrapper.get('/Admin/customer/getAdminInfo')
  },

  // 新增客户
  addCustomer: (customerData) => {
    return apiWrapper.post('/Admin/customer/addCustomer', customerData)
  },

  // 更新客户信息
  updateCustomer: (customerData) => {
    return apiWrapper.put('/Admin/customer/updateCustomer', customerData)
  },

  // 删除客户
  deleteCustomer: (id) => {
    return apiWrapper.delete(`/Admin/customer/deleteCustomer/${id}`)
  },

  // 分页查询客户
  pageCustomer: (queryData, begin, end) => {
    // 构建正确的请求体，匹配CustomerQueryDTO，避免null值导致后端NPE
    const requestBody = {
      username: queryData.username || '',
      pid: queryData.pid || '',
      gender: queryData.gender || '',
      account: queryData.account || '',
      role: queryData.role || '',
      page: queryData.page || 1,
      pageSize: queryData.pageSize || 20
    }

    // 构建查询参数
    const params = new URLSearchParams()
    if (begin) params.append('begin', begin)
    if (end) params.append('end', end)

    const url = `/Admin/customer/pageCustomer${params.toString() ? '?' + params.toString() : ''}`
    
    return apiWrapper.post(url, requestBody)
  },

  // 上传图片
  uploadImage: (file) => {
    const formData = new FormData()
    formData.append('file', file)
    return apiWrapper.post('/Admin/customer/uploadImage', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  }
}

// 兼容旧版本的导出
export const getCustomers = adminCustomerApi.pageCustomer
export const addCustomer = adminCustomerApi.addCustomer
export const updateCustomer = adminCustomerApi.updateCustomer
export const deleteCustomer = adminCustomerApi.deleteCustomer 